// myself/order/order-details.js
import api from '../../utils/api.js';
import network from '../../utils/network.js';
import formatDate from '../../utils/format_date';
import wechat from '../../utils/wechat.js';
import notice from '../../utils/notice.js';
import utils from '../../utils/utils.js';
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        details: {
            siteDO: {
                name: '龙湖天意loft1栋-1101',
                address: '武汉市江岸区高雄路167号台北二路…'
            }
        },
        showOptions: false,
        visibleCancel: false,
        visibleDel: false,
        effective: '00:00'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options);
        this.setData({
            details: JSON.parse(decodeURIComponent(options.order))
        })
        // if (options.orderNo) {
        //     this.loadData(orderNo);
        // } else {
        //     this.setData({
        //         details: JSON.parse(decodeURIComponent(options.order))
        //     })
        //     this.setStatus();
        // }
    },

    loadData(orderNo) {
        network.getJson(api.getCouponOrder, {
            orderNo
        }).then(res => {
            res.data.images = res.data.images.images.split(',');
            this.setData({
                details: res.data
            })

            this.setStatus();
        })
    },
    setStatus() {
        this.getEffective();
        let title = '已完成';
        if (this.data.details.status == 0) {
            title = '待支付';
        } else if (this.data.details.status == 3) {
            title = '已完成';
        } else if (this.data.details.status == 3) {
            title = '已取消';
        } else {
            title = '进行中';
        }
        wx.setNavigationBarTitle({
            title
        })
    },

    payTap() {
        utils.fromEvent().then(() => {
            if (app.globalData.userInfo.mobile) {
                let userInfo = app.globalData.userInfo;
                network.postJson(api.continueOrderCreate, {
                    id: this.data.details.id
                }).then(res => {
                    let orderNo = res.data;
                    network.postJson(api.prepay, {
                        description: this.data.details.couponTypeName,
                        outTradeNo: orderNo,
                        amount: this.data.details.amount,
                        openid: userInfo.openId,
                        orderType: 3 // 1=活动，2=场地，3=卡券
                    }).then(data => {
                        let result = data.data;
                        wx.requestPayment({
                            timeStamp: result.timeStamp,
                            nonceStr: result.nonceStr,
                            package: result.packageVal,
                            signType: result.signType,
                            paySign: result.paySign,
                            success: res => {
                                notice.postNotice("RefreshOrder");
                                let details = this.data.details;
                                details.orderNo = orderNo;
                                details.createTime = new Date().getTime();
                                details.status = 1;
                                this.setData({
                                    details
                                })
                                this.setStatus();
                            },
                            fail: res => {
                                notice.postNotice("RefreshOrder");
                                let details = this.data.details;
                                details.orderNo = orderNo;
                                details.createTime = new Date().getTime();
                                details.status = 0;
                                this.setData({
                                    details
                                })
                                this.setStatus();
                            },
                        })
                    })
                })
            } else {
                wx.navigateTo({
                    url: '/myself/login/login',
                })
            }
        })
    },

    getEffective() {
        if (this.data.details.createTime) {
            let createTime = new Date(this.data.details.createTime).getTime() + (15 * 60 * 1000);
            let now = new Date().getTime();
            let effective = Math.trunc((createTime - now) / 1000);
            if (effective > 0) {
                let timer = setInterval(() => {
                    effective -= 1;
                    if (effective <= 0) {
                        effective == 0;
                        clearInterval(timer);
                        timer = null;
                        this.confirmCancel();
                    }
                    let m = (Math.trunc(effective / 60));
                    let s = effective % 60;
                    if (m < 10) {
                        m = '0' + m;
                    }
                    if (s < 10) {
                        s = '0' + s;
                    }
                    this.setData({
                        effective: m + ':' + s
                    })
                }, 1000)
            }
        }
    },

    openOptions() {
        this.setData({
            showOptions: true
        })
    },

    delOrderTap(e) {
        this.setData({
            showOptions: false,
            visibleDel: true
        })
    },

    confirmDel() {
        network.delJson(api.delCouponOrder, {
            id: this.data.details.id
        }).then(res => {
            notice.postNotice("RefreshOrder");
            wx.showToast({
                icon: 'none',
                title: '删除成功',
            })
            setTimeout(function () {
                wx.navigateBack();
            }, 2000)
        })
    },

    cancelOrderTap(e) {
        this.setData({
            showOptions: false,
            visibleCancel: true
        })
    },

    confirmCancel() {
        network.putJson(api.cancelCouponOrder, {
            id: this.data.details.id
        }, "application/x-www-form-urlencoded").then(res => {
            setTimeout(() => {
                notice.postNotice("RefreshOrder");
                notice.postNotice("ActiveRefresh");
                wx.navigateBack();
            }, 2000)
            this.setData({
                [`details.status`]: 3
            })
            wx.showToast({
                icon: 'none',
                title: '订单取消成功'
            })
        })
    },

    // payTap() {
    //     console.log(this.data.details)
    //     let userInfo = app.globalData.userInfo;
    //     network.postJson(api.prepay, {
    //         description: this.data.details.couponTypeName,
    //         outTradeNo: this.data.details.orderNo,
    //         amount: this.data.details.amount,
    //         openid: userInfo.openId,
    //         orderType: 3 // 1=活动，2=场地，3=卡券
    //     }).then(data => {
    //         let result = data.data;
    //         wx.requestPayment({
    //             timeStamp: result.timeStamp,
    //             nonceStr: result.nonceStr,
    //             package: result.packageVal,
    //             signType: result.signType,
    //             paySign: result.paySign,
    //             success: res => {
    //                 notice.postNotice("ActivePublish", {
    //                     siteId: this.data.site.id,
    //                     name: this.data.site.name,
    //                     accommodate: this.data.site.accommodate,
    //                     couponType: this.data.coupon.couponType
    //                 })
    //                 wechat.navigateBackPage("active/publish/publish");
    //                 // wx.reLaunch({
    //                 //     url: '/active/publish/publish?siteId=' + this.data.details.siteDO.id + '&name=' + this.data.details.siteDO.name + '&accommodate=' + this.data.details.siteDO.accommodate + '&couponId=' + this.data.details.couponId
    //                 // })
    //             },
    //             fail(res) {
    //                 console.log(res)
    //                 // wx.navigateTo({
    //                 //   url: '/myself/order/order-details',
    //                 // })
    //             }
    //         })
    //     })
    // },

    againPayTap() {
        // let coupon = {
        //     // ablePayNumbers: this.data.details.ablePayNumbers,
        //     amount: this.data.details.amount,
        //     couponName: this.data.details.couponTypeName,
        //     id: this.data.details.couponId,
        //     siteId: this.data.details.siteId,
        //     useNumbers: this.data.details.useNumbers
        // }
        network.getJson(api.getCouponByType, {
            couponType: this.data.details.couponType
        }).then(res => {
            let coupon = res.data;
            console.log(res.data)
            wx.navigateTo({
                url: '/site/coupon/coupon?coupon=' + encodeURIComponent(JSON.stringify(coupon)) + '&site=' + encodeURIComponent(JSON.stringify(this.data.details.siteDO))
            })
        })

        // let userInfo = app.globalData.userInfo;
        // network.postJson(api.couponOrderCreate, {
        //     publisherId: userInfo.id,
        //     ableUseNumbers: this.data.details.ableUseNumbers,
        //     amount: this.data.details.amount,
        //     siteId: this.data.details.siteDO.id,
        //     couponId: this.data.details.couponId
        // }).then(res => {
        //     network.postJson(api.prepay, {
        //         description: this.data.details.couponTypeName,
        //         outTradeNo: res.data,
        //         amount: this.data.details.amount,
        //         openid: userInfo.openId,
        //         orderType: 3 // 1=活动，2=场地，3=卡券
        //     }).then(data => {
        //         let result = data.data;
        //         wx.requestPayment({
        //             timeStamp: result.timeStamp,
        //             nonceStr: result.nonceStr,
        //             package: result.packageVal,
        //             signType: result.signType,
        //             paySign: result.paySign,
        //             success: res => {
        //                 wx.reLaunch({
        //                     url: '/active/publish/publish?siteId=' + this.data.details.siteDO.id + '&name=' + this.data.details.siteDO.name + '&accommodate=' + this.data.details.siteDO.accommodate + '&couponId=' + this.data.details.couponId
        //                 })
        //             },
        //             fail(res) {
        //                 console.log(res)
        //                 // wx.navigateTo({
        //                 //   url: '/myself/order/order-details',
        //                 // })
        //             }
        //         })
        //     })
        // })
    }
})